--- Day 10: Elves Look, Elves Say --- Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s). Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1). For example: 1 becomes 11 (1 copy of digit 1). 11 becomes 21 (2 copies of digit 1). 21 becomes 1211 (one 2 followed by one 1). 1211 becomes 111221 (one 1, one 2, and two 1s). 111221 becomes 312211 (three 1s, two 2s, and one 1). Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result? Your puzzle input is 1113122113.

In [5]:
def re_read(in_n):
    out_n = ""
    
    p = in_n[0]
    c_p = 1
    
    for n in in_n[1:]:
        if n != p:
            out_n += "{}{}".format(c_p, p)
            p = n
            c_p = 1
        else:
            c_p += 1
        
    if in_n[-1] == p:
        out_n += "{}{}".format(c_p, p)
        
    return out_n

In [8]:
# Example
token = "1"
print(token)
for i in range(5):
    token = re_read(token)
    print(token)


1
11
21
1211
111221
312211

In [12]:
%%time

# Input
token = "1113122113"
for i in range(40):
    token = re_read(token)
    
print(len(token))


360154
CPU times: user 1.71 s, sys: 4.07 ms, total: 1.72 s
Wall time: 1.72 s
--- Part Two --- Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's Game of Life fame). Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of the new result? Your puzzle input is still 1113122113.

In [13]:
%%time

# Input
token = "1113122113"
for i in range(50):
    token = re_read(token)
    
print(len(token))


5103798
CPU times: user 23.1 s, sys: 20.1 ms, total: 23.1 s
Wall time: 23.1 s

In [ ]: